﻿$(function() {
    AgentCreate.init();
    Utils.textBoxsOnEnter(AgentCreate.Create);
});


var AgentCreate =
{
    init: function() {
        $("#btnSave").click(this.Create);
        $("#loginid").focus();
        $('input[name="supportedProds"]').change(function() { AgentCreate.SwitchDisabled($(this)); });
    }
    , Create: function() {
        var loginid = $("#loginid");
        var username = $("#username");
        var creditlimit = $("#creditlimit");
        var password = $("#password");
        var conpassword = $("#conpassword");
        var possess = $("#possess");

        var parentLoginid = $("#parentLoginid");
        if (parentLoginid.length > 0) {
            loginid.val(parentLoginid.val() + $("#downlinerange").val());
        }

        targetFunction = (parent != undefined) ? parent.ErrorHelper : ErrorHelper;
        targetFunction.checkNonNull(loginid, m.Err0202);
        targetFunction.checkNonNull(username, m.Err0203);
        targetFunction.checkNonNull(creditlimit, m.Err0204);
        targetFunction.checkNonNull(password, m.Err0002);
        targetFunction.checkNonNull(conpassword, m.Err0007);
        targetFunction.checkNonNull(possess, m.Err0207);
        if (!targetFunction.hasError) {
            targetFunction.checkIsEq(password, conpassword, m.Err0008);
            if (targetFunction.hasError) { conpassword.val(''); }
        }
        $('input[name="supportedProdsPrice"]').each(
            function() {
                if (!this.disabled) {
                    if (this.value.length > 0) {
                        if (isNaN(this.value)) {
                            var msg = $(this).parent().children().text() + m.Err9004;
                            targetFunction.setError(msg, $(this));
                        }
                    } else {
                        var msg = $(this).parent().children().text() + m.Err9004;
                        targetFunction.setError(msg, $(this));
                    }
                }
            }
        );

        if (!targetFunction.popUpIfError()) {
            var param = $("#MACreate").serialize();
            var cb = function() { history.go(-2); };
            var ecb = function() { $("#loginid").focus(); };
            utility.ajaxext("AgentService/CreateAgent", param, "POST", cb, ecb);
        }
    }
    , SwitchDisabled: function(obj) {
        var target = obj.parent().parent().children()[0];
        target.disabled = !obj[0].checked;
        if (!target.disabled) {
            target.focus();
        }
    }
};
